package com.qf.controller;
/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/19 10:03
 */

import com.qf.pojo.TbUser;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Base64Utils;
import com.qf.utils.MD5Utils;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;

/**
 * projectName:xiaomimall
 * author:刘哥
 * time:2022/8/19 10:03
 * description:
 */
@WebServlet("/adminLogin")
public class adminUserServlet extends BaseServlet {
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (StringUtil.isEmpty(username)) {
            request.setAttribute("msg", "用户密码不可为空");
            return "redirect:/admin/login.jsp";
        }
        if (StringUtil.isEmpty(password)) {
            request.setAttribute("msg", "用户密码不可为空");
            return "redirect:/admin/login.jsp";
        }

        UserService userService = new UserServiceImpl();
        TbUser admin = userService.selectByUser(username);
        password = MD5Utils.md5(password);
        if (password.equals(admin.getPassword())&&admin.getRole()==0) {

            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);
//                更新登录时间
            CompletableFuture.runAsync(() -> {
                userService.upDateLoginTime(admin.getId());
            });
            return "redirect:/admin/admin.jsp";
        }else {
            return "redirect:/admin/login.jsp";
        }
    }

}
